package com.lu.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lu.mapper.MessagesMapper;
import com.lu.pojo.Messages;
import com.lu.utli.ListSortUtli;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class MessageServiceImpl implements MessageService {
    @Resource
    private MessagesMapper mapper;
    @Override
    public Integer insert(Messages messages) {
        messages.setMtime(new Date());
        int insert = mapper.insert(messages);
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("mfromuserid", messages.getMfromuserid());
        wrapper.eq("mtouserid", messages.getMtouserid());
        Integer integer = mapper.selectCount(wrapper);
        if (insert >= 1) {
            return insert;
        }
        return -1;
    }

    @Override
    public List<Messages> getMessages(int mfromuserid, int mtouserid) {
        QueryWrapper<Messages> objectQueryWrapper = new QueryWrapper<>();
        objectQueryWrapper.orderByDesc("mtime");
        HashMap<String, Object> map = new HashMap<>();
        map.put("mfromuserid",mfromuserid);
        map.put("mtouserid",mtouserid);
        List<Messages> messages = mapper.selectByMap(map);
        map.put("mfromuserid",mtouserid);
        map.put("mtouserid",mfromuserid);
        List<Messages> messages1 = mapper.selectByMap(map);
        messages.addAll(messages1);
        ListSortUtli listSortUtli = new ListSortUtli();
        return ListSortUtli.ListSort(messages);
    }

}
